/**
 * Window 服务类
 */
package cn.antia.cwin.window;

import java.util.*;

import cn.antia.cep.common.pool.*;
import cn.antia.cwin.db.DbService;
import cn.antia.cep.busi.BusiException;
import cn.antia.cwin.NTableConvertor;
import cn.antia.cwin.Output;
import cn.antia.cwin.Cele;
import cn.antia.cwin.grid.AntiaNormalTable;

/**
 * @author sunkeei
 * @date Dec 3, 2007
 * @version 0.1
 * @time 
 */
public class WindowService {
	private AntiaPoolHelper antiaPoolHelper;
	private DbService dbService;
	
	public AntiaPoolHelper getAntiaPoolHelper() {
		return antiaPoolHelper;
	}
	public void setAntiaPoolHelper(AntiaPoolHelper antiaPoolHelper) {
		this.antiaPoolHelper = antiaPoolHelper;
	}
	public DbService getDbService() {
		return dbService;
	}
	public void setDbService(DbService dbService) {
		this.dbService = dbService;
	}
	/**
	 * 根据ID取一行数据
	 * @param winId winId
	 * @param id id
	 * @return List<ElementComsat>
	 */
	public Map<Cele,Object> get(String winId,int id) {
		SingleWindow window = antiaPoolHelper.getSingleWindowNoClone(winId);
		Cele comsat = new Cele();
		comsat.getField().setFieldName("id");
		Map<Cele,Object> conditions = new HashMap<Cele,Object>();
        conditions.put(comsat,id);
		List<Map<Cele,Object>> list = dbService.find(window.getTable(), window.getFieldNames(),conditions );
		if(list != null && list.size() > 0)
			return list.get(0);
		return null;
	}
	
	/**
	 * 查询到数据并返回　装满数据的的表格 AntiaNormalTable
	 * @param winId		window id
	 * @param conditions 查询条件
	 * @param otherElements 其它元素，在此方法中并没有使用，如要使用，请覆盖此方法
	 * @return AntiaTable
	 */
	public AntiaNormalTable query(String winId,Map<Cele,Object> conditions,Map<Cele,Object> otherElements){
		return query(winId,conditions,otherElements,1);
	}
	/**
	 * 查询到数据并返回　装满数据的的表格 AntiaNormalTable
	 * @param winId		window id
	 * @param conditions 查询条件
	 * @param otherElements 其它元素，在此方法中并没有使用，如要使用，请覆盖此方法
	 * @param page 		页数
	 * @return AntiaTable
	 */
	public AntiaNormalTable query(String winId,Map<Cele,Object> conditions,Map<Cele,Object> otherElements,int page){
		SingleWindow window = antiaPoolHelper.getSingleWindowNoClone(winId);
		//总记录
		int recordCount = dbService.findRecordCount(window.getTable(), conditions);
		int ps = window.getPageSize();
		int start = ps * (page - 1);
		int end = start + ps;
		List<Map<Cele,Object>> result= dbService.find(window.getTable(), window.getFieldNames(), conditions,"ID",true,start,end);
		
		AntiaNormalTable antiaTable = getAntiaNormalTable(result,window,start,end);
		//设置页面信息
		antiaTable.setRecordCount(recordCount);		//总页面
		return antiaTable;
	}
	/**
	 * 更新数据，其中更新条件为ID,藏于elements中
	 * @param winId		window id
	 * @param elements	更新的元素
	 * @param otherElements	其它元素，此方法没有实现，如果需要有扩展功能需要操作，请继承覆盖此方法
	 * @throws cn.antia.cep.busi.BusiException e
	 */
	public void update(String winId,Map<Cele,Object> elements,Map<Cele,Object> otherElements)throws BusiException {
		SingleWindow window = antiaPoolHelper.getSingleWindowNoClone(winId);
		dbService.update(window.getTable(), elements);
	}
	/**
	 * 插入一条数据
	 * @param winId		window id
	 * @param elements	插入的元素
	 * @param otherElements	其它元素，此方法没有使用，如果需要有扩展功能需要操作，请继承覆盖此方法
	 * @throws BusiException e
	 */
	public void insert(String winId,Map<Cele,Object> elements,Map<Cele,Object> otherElements)throws BusiException{
		SingleWindow window = antiaPoolHelper.getSingleWindowNoClone(winId);
		dbService.insert(window.getTable(), elements);
	}
	/**
	 * 删除数据 依据单整形主键
	 * @param winId	window id
	 * @param id	主键
	 * @param otherElements  其它元素，此方法没有使用，如果需要有扩展功能需要操作，请继承覆盖此方法
	 * @throws BusiException a
	 */
	public void delete(String winId,int id,Map<Cele,Object> otherElements)throws BusiException{
		SingleWindow window = antiaPoolHelper.getSingleWindowNoClone(winId);
		dbService.delete(window.getTable(), id);
	}
	/**
	 * 删除数据 依据指定条件
	 * @param winId 	window id
	 * @param conditions	条件
	 * @param otherElements	其它元素 ，此方法没有使用，如果需要有扩展功能需要操作，请继承覆盖此方法
	 * @throws BusiException a
	 */
	public void delete(String winId,Map<Cele,Object> conditions,Map<Cele,Object> otherElements)throws BusiException{
		SingleWindow window = antiaPoolHelper.getSingleWindowNoClone(winId);
		dbService.delete(window.getTable(), conditions);
	}
    /**
     * 取得显示表格的数据
     *
     * @param window 表
     * @param rows 行数据,每一行一个List对象
     * @return 表格
     * @throws cn.antia.cwin.ConfigException config
     * @param start start row
     * @param end  end row*/
    private AntiaNormalTable getAntiaNormalTable(List<Map<Cele,Object>> rows,SingleWindow window,int start,int end){
    	AntiaNormalTable table = new AntiaNormalTable();
		table.setRecordCount(rows.size());
		table.setPageSize(window.getPageSize());
		table.setCurrentPage(1);
		table.setPageCount(table.getRecordCount() % table.getPageSize() == 0 ? table.getRecordCount()/table.getPageSize() : table.getRecordCount()/table.getPageSize() + 1);
		if(start < rows.size()){
			end = end < rows.size() ? end : rows.size();
			rows = rows.subList(start, end);
		}else{
			rows = new ArrayList<Map<Cele,Object>>();
		}
		//设置数据行
		//取出FieldTd list
		List<Output> outputList = new ArrayList<Output>();
		List<Cele> fields = window.getFields();
        for (Cele field : fields) {
            outputList.add( field.getTd());
        }
        table.setRows(NTableConvertor.getDataList(outputList,window.getExtraColumns(),rows));
		table.setWindowId(window.getId());
		table.setId("tw_" + window.getId());
		//表头
		table.setTh(NTableConvertor.getTableTH(outputList,window.getExtraColumns(),table.getId()));
		//表描述
		table.setDesc(window.getDesc());
		return table;
	}
}
